﻿using Common.Library;
using Microsoft.Phone.Scheduler;
using Microsoft.Phone.Shell;
using OnlineVideos.Common;
using OnlineVideos.Data;
using OnlineVideos.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace OnlineVideos.Library
{
   public static class LiveTileUpDate
    {
     
       public static void LiveTileForCycle()
       {
           
            List<ShowList> downloadrecentlist = null;
           int downloadHistory;
           downloadrecentlist = OnlineShow.GetRecentlyAddedShows().Items.ToList();
           List<Uri> cycleimagelist = new List<Uri>();
           downloadHistory = downloadrecentlist.Count();
           if (Task.Run(async () => await Storage.FileExists("Historys.xml")).Result)
           {
               XDocument xdoc = Storage.ReadFileAsDocument("Historys.xml");

               var data = from c in xdoc.Descendants("Historys") select c;

               SyncAgentState.HistoryCount = data.Distinct().Count();
               AppSettings.LiveTileHistroryCount = data.Distinct().Count();
           }
           else
           {
               SyncAgentState.HistoryCount = downloadHistory;
               AppSettings.LiveTileHistroryCount = downloadHistory;
           }
            foreach (var item in downloadrecentlist)
           {
               string ImageUrl =ResourceHelper.GetLiveTileUrl(item.TileImage);
              Uri img = new Uri(ImageUrl, UriKind.RelativeOrAbsolute);
              cycleimagelist.Add(img);
            }
           
               ShellTile primaryTile = ShellTile.ActiveTiles.FirstOrDefault();
               CycleTileData cycleTile = new CycleTileData();
                cycleTile.Title =ResourceHelper.ProjectName;
                cycleTile.Count=AppSettings.LiveTileHistroryCount;
                cycleTile.SmallBackgroundImage = new Uri("/Assets/Tiles/logo159x159.png", UriKind.Relative);
                cycleTile.CycleImages = cycleimagelist;
            try
            {
                if (primaryTile != null)
                {
                    primaryTile.Update(cycleTile);
                   

                }
            }
            catch (Exception ex)
            {
                if (Constants.LiveTileBackgroundAgentStatus == true)
                {
                    //if (AppSettings.ProjectName == "Story Time" || AppSettings.ProjectName == "Vedic Texts")
                    //{
                    //    AppSettings.BackgroundAgentStatus = SyncAgentStatus.UploadStory;
                    //}
                    //else
                        AppSettings.BackgroundAgentStatus = SyncAgentStatus.UploadSearch;
                }
            }
            AppSettings.LiveTileBackgroundAgentStatus = true;
            if (Constants.LiveTileBackgroundAgentStatus == true)
            {
                //if (AppSettings.ProjectName == "Story Time" || AppSettings.ProjectName == "Vedic Texts")
                //{
                //    Exceptions.UpdateAgentLog("UpDateLiveTile Completed in LiveTileUpdate.cs ");
                //    AppSettings.BackgroundAgentStatus = SyncAgentStatus.UploadStory;
                //}
                //else
                //{
                    Exceptions.UpdateAgentLog("UpDateLiveTile Completed in LiveTileUpdate.cs ");
                    AppSettings.BackgroundAgentStatus = SyncAgentStatus.UploadSearch;
                //}
            }
          
        }
    }
}
